package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStoreService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/stores")
public class StoreController {

    @Autowired
    private IStoreService storeService;

    @Autowired
    private IUserService userService;
    /*
    查询商品全部信息
    资源:/stores
    动作:GET
    参数:
     */
    @GetMapping
    public Object getList(QueryObject qo){
        return storeService.query(qo);
    }

    /*
    根据当前登录人id,查询当前id用户的信息积分
    资源:/stores/{userId}/score
    动作:GET
    参数:
     */

    @GetMapping("/{userId}/score")
    public Object getByUserId(@PathVariable Long userId){
        return userService.getByUserId(userId);
    }

    /*
    查询每个商品信息
    资源:/stores/{id}
    动作:GET
    参数:
     */
    @GetMapping("{id}")
    public Object getStoreId(@PathVariable Long id){
        return storeService.getStoreId(id);
    }


    @PostMapping("/products/{goodsId}/users/{userId}")
    public Object buyAProducts(@PathVariable Long goodsId,@PathVariable Long userId){
        JSONResult result = new JSONResult();
        try {
         Object object =storeService.buyAProducts(goodsId,userId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
